var app=getApp()
var util=require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    shengfen:[],
    classTime:'',
    sex:'',
    latitude:'',
    longitude:'',
    subjectId:'',
    gradeId:'',
    gradesArray:[
      {
        id:1,
        value:'幼小'
      },
      {
        id: 2,
        value: '小学'
      },
      {
        id: 3,
        value: '初中'
      },
      {
        id: 4,
        value: '高中'
      },
      {
        id: 5,
        value: '大学'
      },
      {
        id:6,
        value: '其他'
      }
     
    ],
     subjectsArray:[
    //   id:'yuwen',
    //   value:'语文'
    // },
    //   {
    //     id: 'shuxue',
    //     value: '数学'
    //   },
    //   {
    //     id: 'yingyu',
    //     value: '英语'
    //   },
    //   {
    //     id: 'wuli',
    //     value: '物理'
    //   },
    //   {
    //     id: 'huaxue',
    //     value: '化学'
    //   },
    //   {
    //     id: 'shengwu',
    //     value: '生物'
    //   },
    //   {
    //     id: 'zhengzhi',
    //     value: '政治'
    //   },
    //   {
    //     id: 'lishi',
    //     value: '历史'
    //   },
    //   {
    //     id: 'dili',
    //     value: '地理'
    //   },
      ],
    teacherIdentity:'',
    address: '请选择您所在的地址',
    phoneNumber: '短信验证',
    titleStyle: 'font-size:15px; color:#434343;font-weight:normal;',
    imgStyle: 'width:28rpx; height:34rpx;',
    phoneStyle: 'font-size:15px; color:#626262;font-weight:normal;',
    courseName:'',
    isKnow:''
  },
  getClassTime:function(event){
    if(event.detail.value==''){
      wx.showToast({
        icon:'none',
        title: '请输入您的上课时间'
      })
    }else{
      this.setData({
        classTime: event.detail.value
      })
    }
  
  },
  isKnow:function(event){
    this.setData({
      isKnow:event.detail.k
    })
  },
  yanzhen:function(event)
  {
    wx.navigateTo({
      url: '/fabu/duanxin/duanxin',
    })
  },
  getName:function(res){
  
      this.setData({
        name: res.detail.value
      })
    
   
  },
  getSubject: function (res) {
     this.setData({
      subjectId:res.detail.id
     })
  },
  getGrade: function (res) {
    var subjectId = res.detail.id
  
    console.log(subjectId)
    if(subjectId==''){
      wx.showToast({
        title: '请选择您教授的年级',
        icon:'none'
      })
    }else{
      this.setData({
        gradeId: subjectId
      })
    }
    
    // console.log(this.data.subId)
  },
  getsex:function(res){
    var s = res.detail.id
   
      var sexNum
      console.log(s)
      if (s == 'man') {
        sexNum = 1
      } else if (s == 'woman') {
        sexNum = 2
      }
      this.setData({
        sex: sexNum
      })
    
  
  },
  chooseAdd:function(event)
  {
    var that=this
    var location
    var add
    //这是一个异步函数
    wx.chooseLocation({
      success: function (res) {
        var add=res.address
        console.log(add.length)
        if(add.length>20)
        {
          add=res.name
        }
        that.setData({
          address: add,
          latitude:res.latitude,
          longitude:res.longitude
        })
      
      },
    })
   
  },
  getIdentity:function(event){
    var identity=event.detail.name
 
      this.setData({
        teacherIdentity: identity
      })
   
   

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    wx.request({
      url: app.globalData.baseUrl+'demand/listcourse',
      method:'get',
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },
      success:function(res){
        console.log(res)
        for(var index=0;index<res.data.data.data.length;index++){
          var id='subjectsArray['+index+'].id'
          var value='subjectsArray['+index+'].value'
          that.setData({
            [id]:res.data.data.data[index].id,
            [value]:res.data.data.data[index].name
          })
        }
      }
    })
    wx.request({
      url: app.globalData.baseUrl+'teacher/type',
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },
      success:function(res){
        console.log(res)
        var resData=res.data.data.data
        for(var i=0;i<resData.length;i++){
          var id='shengfen['+i+'].id'
          var value='shengfen['+i+'].value'
          that.setData({
            [id]:resData[i].id,
            [value]:resData[i].name
          })
        }
      }
    })
  },
  toDemand:function(){
    var that=this
    var time = util.formatTime(new Date())
    var code = wx.getStorageSync('checktxt')
    console.log(code)
    if (this.data.isKnow) {
      wx.request({
        url: app.globalData.baseUrl + 'demand/insert',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
        },
        method: 'POST',
        data: {
          title: that.data.name,
          startTime: time,
          endTime: time,
          type: 2,
          special: that.data.classTime,
          gradeId: that.data.gradeId,
          courseId: that.data.subjectId,
          position: that.data.address,
          latitude: that.data.latitude,
          longitude: that.data.longitude,
          teacherGender: that.data.sex,
          identity: that.teacherIdentity,
          smsCode: code
        },
        success: function (res) {
          console.log(res)
          if (res.data.msg == '请求成功') {
            wx.showToast({
              title: '发布成功',
            })
            wx.navigateTo({
              url: '/xuqiu/xuqiu/xuqiu',
            })
          } else {
            wx.showToast({
              title: '发布失败,原因:' + res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    } else {
      wx.showModal({

        content: '请查看免责声明',
      })
    }
  },
  demand:function(event){
    var checkTxt=wx.getStorageSync('checktxt')
    if(checkTxt==''){
      wx.showToast({
        icon: 'none',
        title: '请填写填写验证码',
      })
    }else if(this.data.name==''){
      wx.showToast({
        title: '请输入您的姓名',
        icon: 'none'
      })
    }else if(this.data.sexNum==''){
      wx.showToast({
        title: '请选择您的性别',
        icon: 'none'
      })
    }else if(this.data.gradeId==''){
      wx.showToast({
        title: '请选择您所教授的年级',
        icon:'none'
      })
    } else if (this.data.subjectId == '') {
      wx.showToast({
        title: '请选择您所教授的科目',
        icon:'none'
      })
    }else if(this.data.address=='请选择您所在的地址'){
      wx.showToast({
        title: '请选择您所地址',
        icon: 'none'
      })
    }else if(this.data.identity==''){
      wx.showToast({
        title: '请选择您的身份',
        icon: 'none'
      })
    }
    else{
      this.toDemand()
    }
   
     
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})